<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\type;
use app\models\topic;
use app\models\topicState;

class TopicController extends Controller{
    public $enableCsrfValidation = false; 
	 //搜索问题关键字！ 
    public function actionSearch(){
	      $model = new topic();
		  if(Yii::$app->request->isPost){
		    $postData = Yii::$app->request->post();
		    $content = $postData['content'];
                    $isAll = $postData['isAll'];
                    if( $isAll == 0 ){
		    $sql = "select * from freshmanHelper_topic where apply = '1' and content like '%$content%'";
                    $data = topic::findBysql($sql)->asArray()->all();
                    echo json_encode(array('error' => 0, 'message' => '新生','data' => $data,));
                 } else{
                    $sql = "select * from freshmanHelper_topic where content like '%$content%'";
                    $data = topic::findBysql($sql)->asArray()->all();
                    echo json_encode(array('error' => 0, 'message' => '全部','data' => $data,));   
                   }
  		}
                    exit(0);
	}
	
	  //2.5查看问题对应的文章
	 public function actionAnswer(){
		 $model = new topic();
		 if(Yii::$app->request->isPost){
			 $postData = Yii::$app->request->post();
			 $topicId = $postData['topicId'];
			 $sql = "select topicId,content,answer from freshmanHelper_topic where topicId = $topicId";
			 $data = topic::findBysql($sql)->asArray()->all();
			 echo json_encode(array('error' => 0, 'message' => 'success','data' => $data,));
		   }
                         exit(0);  
	}
	  //更改问题topic状态（0未解决 / 1已解决 / -1已反馈）
	  //topic表score积分++
	public function actionUpstate(){
		$model = new topicState();
		$topic = new topic();
		if(Yii::$app->request->isPost){
			$postData = Yii::$app->request->post();
                        $result = $model->add($postData);
                        if( $result == 0){
                           $data = array('content' => '提交数据成功');
                           echo json_encode(array('error' => 0,'message' => 'success','data'=>$data,));
                       }else{
                           $data = array('content' => '提交数据失败','Content' => $result);
                           echo json_encode(array('error' => 1,'message' =>'fail','data' => $data,));
                     }
	    }
                       exit(0);
       }
         //所有疑惑的数量
        public function actionNum(){
             $model = new topic();
             $sql = "select count(topicId) from freshmanHelper_topic";
             $data = topic::findBysql($sql)->asArray()->all();
             $data = (int)$data[0]['count(topicId)'];
             echo json_encode(array('error' => 0,'message' =>'success','TopicNum' =>$data,));
             exit(0);
        }  
  }

